package com.quyeying.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.quyeying.entity.TblMerchantEntity;
import com.quyeying.entity.TblProductEntity;
import com.quyeying.service.TblMerchantService;
import com.quyeying.service.TblProductService;
import com.quyeying.utils.JwtUtil;
import com.quyeying.utils.TokenUtil;
import com.quyeying.utils.result.R;
import com.quyeying.utils.result.ResultEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Primary;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
* <p>
* tbl_product 前端控制器
* </p>
* @author jll
* @since 2022-07-19
*/
@Slf4j
@RestController
@RequestMapping("productEntity")
public class TblProductController {

    @Resource
    private TblProductService tblProductService;
    @Resource
    private TblMerchantService tblMerchantService;

    /**
    * 新增
    **/
    @PostMapping("/add")
    public R add(@Validated @RequestBody TblProductEntity tblProductEntity, HttpServletRequest request){
        try {
            String authorization = request.getHeader("Authorization");
            String userName = JwtUtil.getUserName(authorization);
            TblMerchantEntity merchantInfo = tblMerchantService.getMerchantInfo(userName);
            tblProductEntity.setMerchantId(merchantInfo.getId());
            return tblProductService.add(tblProductEntity);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return R.error(ResultEnum.SYSTEM_ERROR);
        }
    }

    /**
    * 删除
    **/
    @DeleteMapping("/del")
    public R delete(Long id){
        try {
            return tblProductService.delete(id);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return R.error(ResultEnum.SYSTEM_ERROR);
        }
    }

    /**
    * 更新
    **/
    @PostMapping("/update")
    public R update(@RequestBody TblProductEntity tblProductEntity){
        try {
            return tblProductService.updateData(tblProductEntity);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return R.error(ResultEnum.SYSTEM_ERROR);
        }
    }

    /**
    * 分页查询
    **/
    @GetMapping("/findListByPage")
    public R findListByPage(@RequestParam Integer current,
    @RequestParam Integer size,@RequestParam Integer productBuyZt,HttpServletRequest request){
        try {
            String authorization = request.getHeader("Authorization");
            String userName = JwtUtil.getUserName(authorization);
            TblMerchantEntity merchantInfo = tblMerchantService.getMerchantInfo(userName);
            return tblProductService.findListByPage(current, size, productBuyZt, merchantInfo.getId());
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return R.error(ResultEnum.SYSTEM_ERROR);
        }
    }

    /**
    * id查询
    **/
    @GetMapping("/findById")
    public R findById(Long id, String isSys){
        try {
            return tblProductService.findById(id,"", 0L, isSys);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return R.error(ResultEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 商品名搜索
     **/
    @GetMapping("/searchText")
    public R searchText(String text){
        try {
            QueryWrapper<TblProductEntity> productEntityQueryWrapper = new QueryWrapper<>();
            productEntityQueryWrapper.select("id, product_name as productName").like("product_name", text);
            List<Map<String, Object>> list = tblProductService.listMaps(productEntityQueryWrapper);
            return R.success(list);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return R.error(ResultEnum.SYSTEM_ERROR);
        }
    }
}
